import 'dart:convert';

/// CreateTime 2020/4/21 19:51
/// CreateUser chen 
/// ProjectName flutter_elf
import 'package:flutter_elf/config/component_index.dart';
import 'package:flutter_elf/network/entity_factory.dart';
import 'package:sqflite/sqflite.dart';
import 'package:convert/convert.dart' show json;
import 'base_db.dart';


class BaseDBData<T> extends BaseDb{
  final List<String> columnName;
  BaseDBData(this.columnName);
  static const COLUMN_ID = 'id';
  @override
  String createSqlStr() {

    String columnStr = columnName.map((str){
      return str + ' TEXT';
    }).toList().join(',');
    return '''
        create table ${getTableName()} (
          $COLUMN_ID INTEGER primary key autoincrement,
          $columnStr
        )
      ''';
  }

  Future<bool> exist(dynamic arg)async{
    if(arg==null)return false;
    Database db = await getDB();
    List<Map<String, dynamic>> maps = await db
        .query(getTableName(), where: "$COLUMN_ID = ?", whereArgs: [arg]);
    return maps.isNotEmpty;
  }

  Future getDataByArg(String arg)async{
    bool _isExist = await exist(arg);
    if (!_isExist) return null;
    List<T> books = [];
    Database db = await getDB();
    List<Map<String, dynamic>> maps = await db
        .query(getTableName(), where: "$COLUMN_ID = ?", whereArgs: [arg]);
    if (maps.isNotEmpty) {
      for (Map<String, dynamic> map in maps) {
        T t = EntityFactory.generateOBJ(json.encode(map));
        books.add(t);
      }
      return books.first;
    } else {
      return null;
    }
  }

  Future<List<T>> getAllData() async {
    List<T> books = [];
    Database db = await getDB();
    List<Map<String, dynamic>> maps = await db.query(getTableName());
    if (maps.isNotEmpty) {
      for (Map<String, dynamic> map in maps) {
        T book = EntityFactory.generateOBJ(map);
        books.add(book);
      }
    }
    return books;
  }


  Future<int> insertOrReplaceToDB(T t,dynamic arg) async {
    if (t == null) return -1;
    if (t == null || arg == null) return -1;
    bool _isExist = await exist(arg);
    if (!_isExist) {}
    Database db = await getDB();
    return await db.insert(getTableName(), json.decode(t.toString()));
  }


  Future<bool> updateBook(T t,dynamic arg) async {
    if (t == null || arg == null) return false;
    bool _isExist = await exist(arg);
    if (!_isExist) return false;
    Database db = await getDB();
    int result = await db
        .update(getTableName(), json.decode(t.toString()), where: "$COLUMN_ID = ?", whereArgs: [arg]);
    return result == 1;
  }
  Future<bool> deleteBook(dynamic arg) async {
    bool _isExist = await exist(arg);
    if (!_isExist) return false;
    Database db = await getDB();

    int result = await db
        .delete(getTableName(), where: "$COLUMN_ID = ?", whereArgs: [arg]);

    return result == 1;
  }


  clear()async{
    Database db = await getDB();
    int result = await db.delete(getTableName());
    return result == 1;
  }

  @override
  String getTableName() {
    // TODO: implement getTableName
    return this.runtimeType.toString().substring(0,this.runtimeType.toString().indexOf('<'));
  }
}